package mortal.game.control
{
    import flash.events.*;
    import modules.*;
    import mortal.component.window.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.*;
    import mortal.game.rules.*;
    import mortal.game.scene.*;
    import mortal.game.scene.events.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class MountsController extends Controller
    {

        public function MountsController()
        {
            
        }

        private function onCallMountsReq(event:DataEvent) : void
        {
            var event:* = event;
            if (RolePlayer.instance.isDoubleRest)
            {
                ScreenOperation.interruptDoubleRest(function () : void
            {
                onMountsCalling(event.data as ItemData);
                
            }
            );
                
            }
            this.onMountsCalling(event.data as ItemData);
            
        }

        private function onMountsCalling(param1:ItemData) : void
        {
            if (param1 == null)
            {
                
            }
            if (cache.status.currentStatus == StatusType.AutoFight)
            {
                MsgManager.addBroadCast("挂机中无法召唤");
                MsgManager.addTipText("挂机中无法召唤");
                
            }
            if (RolePlayer.instance.fighting)
            {
                MsgManager.addBroadCast("战斗中无法召唤");
                MsgManager.addTipText("战斗中无法召唤");
                
            }
            if (param1.itemInfo.item.level > cache.role.entityInfo.level)
            {
                MsgManager.addBroadCast("等级不足无法召唤");
                MsgManager.addTipText("等级不足无法召唤");
                
            }
            if (SkillProgress.instance.running && SkillProgress.instance.type == ProcessType.Hunt)
            {
                MsgManager.addBroadCast("挖宝中无法召唤");
                MsgManager.addTipText("挖宝中无法召唤");
                
            }
            if (cache.guide.transportIng)
            {
                MsgManager.addBroadCast("灵兽护送中无法召唤");
                MsgManager.addTipText("灵兽护送中无法召唤");
                
            }
            cache.pack.mountsPackCache.callingMount = param1;
            if (param1.itemInfo.extend.costTime == 0)
            {
                this.onMountsCallEnd();
            }
            else
            {
                SkillProgress.instance.updateData(ProcessType.Mounts, "正在召唤坐骑", param1.itemInfo.extend.costTime, this.onMountsCallEnd);
            }
            RolePlayer.instance.addEventListener(RolePlayerEvent.ControlStateChange, this.onMountCallInterruptHander);
            
        }

        private function onMountsCallEnd() : void
        {
            RolePlayer.instance.removeEventListener(RolePlayerEvent.ControlStateChange, this.onMountCallInterruptHander);
            if (cache.pack.mountsPackCache.callingMount != null)
            {
                GameProxy.mountsProxy.call(cache.pack.mountsPackCache.callingMount.uid);
            }
            cache.pack.mountsPackCache.callingMount = null;
            
        }

        private function onUnCallMounts(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            if (_loc_2)
            {
                GameProxy.mountsProxy.unCall(_loc_2.uid);
            }
            else
            {
                _loc_2 = Cache.instance.pack.mountsPackCache.callEdMount;
                if (_loc_2)
                {
                    GameProxy.mountsProxy.unCall(_loc_2.uid);
                }
                else if (SkillProgress.instance.running && SkillProgress.instance.type == ProcessType.Mounts)
                {
                    SkillProgress.instance.dispose();
                }
            }
            
        }

        private function onMountsUnEquipReq(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            if (_loc_2 != null)
            {
                GameProxy.mountsProxy.unEquip(_loc_2.uid);
            }
            
        }

        private function onMountsEquipReq(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            if (_loc_2 != null)
            {
                GameProxy.mountsProxy.equip(_loc_2.uid);
            }
            
        }

        private function onMountsDelReq(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            if (_loc_2 != null)
            {
                GameProxy.mountsProxy.remove(_loc_2.uid);
            }
            
        }

        private function onKeyControllCallMounts(event:DataEvent) : void
        {
            var evt:* = event;
            if (RolePlayer.instance.isDoubleRest)
            {
                ScreenOperation.interruptDoubleRest(function () : void
            {
                callMounts();
                
            }
            );
                
            }
            this.callMounts();
            
        }

        private function callMounts() : void
        {
            var _loc_1:* = Cache.instance.pack.mountsPackCache.equipEdMount;
            if (!_loc_1)
            {
                if (cache.role.entityInfo.level >= 14)
                {
                    MsgManager.addBroadCast("没有装备坐骑");
                }
                
            }
            var _loc_2:* = Cache.instance.pack.mountsPackCache.callEdMount;
            if (_loc_2 == null)
            {
                this.onMountsCalling(_loc_1);
            }
            else if (_loc_2 != null)
            {
                GameProxy.mountsProxy.unCall(_loc_1.uid);
            }
            
        }

        protected function onMountsDataHandler(param1:Array) : void
        {
            this.updateView(param1);
            
        }

        private function onMountsAddRes(param1:ItemData) : void
        {
            this.updateView(cache.pack.mountsPackCache.getAllItems());
            
        }

        private function onMountsDelRes(param1:ItemData) : void
        {
            this.updateView(cache.pack.mountsPackCache.getAllItems());
            
        }

        private function onMountsEquipRes(param1:ItemData) : void
        {
            this.updateView(cache.pack.mountsPackCache.getAllItems());
            
        }

        private function onMountsUnEquipRes(param1:ItemData) : void
        {
            this.updateView(cache.pack.mountsPackCache.getAllItems());
            
        }

        private function onMountsCallRes(param1:ItemData) : void
        {
            this.updateView(cache.pack.mountsPackCache.getAllItems());
            
        }

        private function onMountsUnCallRes(param1:ItemData) : void
        {
            this.updateView(cache.pack.mountsPackCache.getAllItems());
            
        }

        private function onMountsSaleRes(param1:ItemData) : void
        {
            this.updateView(cache.pack.mountsPackCache.getAllItems());
            
        }

        private function updateView(param1:Array) : void
        {
            view.update(param1);
            
        }

        private function onWindowShow(event:WindowEvent) : void
        {
            _view.removeEventListener(WindowEvent.SHOW, this.onWindowShow);
            _view.update(cache.pack.mountsPackCache.getAllItems());
            
        }

        private function onMountCallInterruptHander(event:Event) : void
        {
            var _loc_2:Boolean = false;
            if (cache.pack.mountsPackCache.callingMount)
            {
                _loc_2 = event is RolePlayerEvent;
                if (!_loc_2 || !Game.scene.rolePlayer.isCanControlReleaseMagic)
                {
                    if (SkillProgress.instance.running && SkillProgress.instance.type == ProcessType.Mounts)
                    {
                        SkillProgress.instance.dispose();
                        MsgManager.addBroadCast("召唤被打断");
                        cache.pack.mountsPackCache.callingMount = null;
                    }
                }
            }
            RolePlayer.instance.removeEventListener(RolePlayerEvent.ControlStateChange, this.onMountCallInterruptHander);
            
        }

        override protected function initView() : IView
        {
            var _loc_1:* = new MountsModule();
            _loc_1.addEventListener(WindowEvent.SHOW, this.onWindowShow);
            return _loc_1;
        }

        override protected function initServer() : void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.MountsListRes, this.onMountsDataHandler);
            NetDispatcher.addCmdListener(ServerCommand.MountsAddRes, this.onMountsAddRes);
            NetDispatcher.addCmdListener(ServerCommand.MountsDelRes, this.onMountsDelRes);
            NetDispatcher.addCmdListener(ServerCommand.MountsCallRes, this.onMountsCallRes);
            NetDispatcher.addCmdListener(ServerCommand.MountsUnCallRes, this.onMountsUnCallRes);
            NetDispatcher.addCmdListener(ServerCommand.MountsEquipRes, this.onMountsEquipRes);
            NetDispatcher.addCmdListener(ServerCommand.MountsUnEquipRes, this.onMountsUnEquipRes);
            NetDispatcher.addCmdListener(ServerCommand.MountsSaleRes, this.onMountsSaleRes);
            Dispatcher.addEventListener(EventName.CallMounts, this.onCallMountsReq);
            Dispatcher.addEventListener(EventName.MountsUnEquipReq, this.onMountsUnEquipReq);
            Dispatcher.addEventListener(EventName.MountsEquipReq, this.onMountsEquipReq);
            Dispatcher.addEventListener(EventName.MountsDelReq, this.onMountsDelReq);
            Dispatcher.addEventListener(EventName.KeyControllMounts, this.onKeyControllCallMounts, false, 0, true);
            Dispatcher.addEventListener(EventName.UnCallMounts, this.onUnCallMounts, false, 0, true);
            
        }

    }
}
